package com.zcs.controller;


import com.zcs.pojo.Books;
import com.zcs.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : zcs
 * Data :  2021/8/21 9:36
 */
@Controller
@RequestMapping("/book")
public class BookController {

    //controller层 调service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询所有的书籍，并且返回到一个书籍展示页面

    @GetMapping("/allBook")
    public String list(Model model){
        System.out.println("dadasd");
        List<Books> booksList=bookService.queryAllBook();
        model.addAttribute("list",booksList);
         return "allBook";
    }

    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }


    /**
     * 添加书籍的请求
     * */
    @RequestMapping("/addBook")
    public String addBook( Books books){
        System.out.println("addBook==>"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";  //重定向到我们的RequestMapping("/allBook")请求;
    }

    //跳转到修改页面
    @RequestMapping("/toUpdate")
    public String toEditPaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("book",books);
        return "updateBook";
    }

    /**
     *更新页面的信息
     * */
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        System.out.println("addBook==>"+books);
        bookService.updateBook(books);
        return "redirect:/book/allBook";  //重定向到我们的RequestMapping("/allBook")请求;
    }


    /**
     * 删除
     * */
    @RequestMapping("/del/{bookId}")
    public String deleteBook(@PathVariable("bookId") int id){
        System.out.println(id);
        int i = bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }

    /**
     * 查询书籍
     * */
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        List<Books> books = bookService.queryBookByName(queryBookName);
        model.addAttribute("list",books);
        return "allBook";
    }

}
